// ===================================string============================
/**
 * URI字符串编码
 * @param value {string}
 * @returns {*}
 */
;String.prototype.encode = function () {
    var value = this;
    if (value == "")return value;
    return encodeURIComponent(encodeURIComponent(value));
};
/***
 * URI字符串解码
 * @param value {string}
 * @returns {*}
 */
String.prototype.decode = function () {
    var value = this;
    if (value == "")return value;
    return decodeURIComponent(decodeURIComponent(value));
    ;
};
/**
 * HTML字符串编码
 * @param value {string}
 * @returns {string}
 */
String.prototype.encodeHTML = function (value) {
    return String(value)
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#39;");
};
/**
 * HTML字符串解码
 * @param value {string}
 * @returns {string}
 */
String.prototype.decodeHTML = function (value) {
    var str = String(value)
        .replace(/&quot;/g, '"')
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&amp;/g, "&");
    return str.replace(/&#([\d]+);/g, function (_0, _1) { //处理转义的中文和实体字符
        return String.fromCharCode(parseInt(_1, 10));
    });
};
/**
 * 字符串是否以suffix结尾后缀
 * @param value {string}
 * @param suffix {string}
 * @returns {boolean}
 */
String.prototype.endWith = function (suffix) {
    var value = this;
    return suffix.length > 0 && value.substring(value.length - suffix.length, value.length) === suffix;
};
/**
 * 字符串是否以suffix开头前缀
 * @param value
 * @param suffix
 * @returns {boolean}
 */
String.prototype.startWith = function (suffix) {
    var value = this;
    return prefix.length > 0 && value.substring(0, prefix.length) === prefix;
}
String.prototype.trim = function () {
    return this.replace(/^\s+/g, "").replace(/\s+$/g, "");
};
String.prototype.getLen = function () {
    ///<summary>获得字符串实际长度，中文2，英文1</summary>
    ///<param name="str">要获得长度的字符串</param>
    var str = this;
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128) realLength += 1;
        else realLength += 2;
    }
    return realLength;
};
/**
 * 将字符串转换为日期;格式化失败则获取当前日期
 * @param {String} format 默认格式：yyyy-MM-dd 有效格式:[yyyy]年[MM]月[dd]日 时间如：hh:mm:ss
 */
String.prototype.toDate = function (format) {
    format = format || 'yyyy-MM-dd';
    pattern = format.replace(/(yyyy)/g, "([0-9]{4})")
        .replace(/(yy)|(MM)|(dd)|(hh)|(mm)|(ss)/g, "([0-9]{2})")
        .replace(/[Mdhms]/g, "([0-9]{1,2})");

    //获取子表达式的索引
    getIndex = function (expr1, expr2) {
        var index = format.indexOf(expr1);
        if (index == -1) index = format.indexOf(expr2);
        return index;
    }

    var today = new Date();
    var returnDate;
    if (new RegExp(pattern).test(this)) {
        var yPos = getIndex("yyyy", "yy");
        var mPos = getIndex("MM", "M");
        var dPos = getIndex("dd", "d");
        var hPos = getIndex("hh", "h");
        var miPos = getIndex("mm", "m");
        var sPos = getIndex("ss", "s");
        var data = { y: 0, m: 0, d: 0, h: 0, mi: 0, s: 0 };
        //对索引进行排序
        var pos = new Array(yPos + ",y", mPos + ",m", dPos + ",d", hPos + ",h", miPos + ",mi", sPos + ",s").sort(function (a, b) {
            a = parseInt(a.split(",")[0]);
            b = parseInt(b.split(",")[0]);
            return a > b;
        });

        //删除索引为-1的数组
        var tmpIndex = 0;
        var newPos = new Array();
        for (var i = 0; i < pos.length; i++) {
            if (parseInt(pos[i].split(",")[0]) != -1) {
                newPos[tmpIndex] = pos[i];
                tmpIndex++;
            }
        }

        //与当前文本进行匹配
        var m = this.match(pattern);
        for (var i = 1; i < m.length; i++) {
            if (i == 0) return;
            var flag = newPos[i - 1].split(',')[1];
            data[flag] = m[i];
        }
        ;

        data.y = data.y || today.getFullYear();             //年如果为空，则取当前年
        data.d = data.d || today.getDate();                 //天如果为空，则取今天
        //月不需要处理，因为月的0月是1月

        //如果年是yy格式，则加上20
        if (data.y.toString().length == 2) data.y = parseInt('20' + data.y);
        data.m -= 1;
        returnDate = new Date(data.y, data.m, data.d, data.h, data.mi, data.s);
    }
    returnDate = returnDate || today;
    return returnDate;
};